﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BasePlatformCommon;

namespace BasePlatformNet {
    public enum ArtPacketType : ushort {
        Invalid = 0x0,
        OpPoll = 0x2000,
        OpPollReply = 0x2100,
        OpOutput = 0x5000,
        OpTodRequest = 0x8000,
        OpTodData = 0x8100,
        OpTodControl = 0x8200,
        OpRDM = 0x8300,
        OpRDMSub = 0x8400,
    }

    public enum CommandType : byte {
        DISCOVERY_COMMAND = 0x10,
        GET_COMMAND = 0x20,
        SET_COMMAND = 0x30,
        DISCOVERY_COMMAND_RESPONSE = 0x11,
        GET_COMMAND_RESPONSE = 0x21,
        SET_COMMAND_RESPONSE = 0x31,
    }

    public enum Catergory : ushort {
        //   Category – Network Management
        DISC_UNIQUE_BRANCH = 0x0001,
        DISC_MUTE = 0x0002,
        DISC_UN_MUTE = 0x0003,
        PROXIED_DEVICES = 0x0010,
        PROXIED_DEVICE_COUNT = 0x0011,
        COMMS_STATUS = 0x0015,

        //   Category - Status Collection
        QUEUED_MESSAGE = 0x0020,
        STATUS_MESSAGES = 0x0030,
        STATUS_ID_DESCRIPTION = 0x0031,
        CLEAR_STATUS_ID = 0x0032,
        SUB_DEVICE_STATUS_REPORT_THRESHOLD = 0x0033,

        //   Category - RDM Information
        SUPPORTED_PARAMETERS = 0x0050,
        PARAMETER_DESCRIPTION = 0x0051,

        //   Category – Product Information
        ICOLOR_INFO = 0x8000,
        DEVICE_INFO = 0x0060,
        PRODUCT_DETAIL_ID_LIST = 0x0070,
        DEVICE_MODEL_DESCRIPTION = 0x0080,
        MANUFACTURER_LABEL = 0x0081,
        DEVICE_LABEL = 0x0082,
        FACTORY_DEFAULTS = 0x0090,
        LANGUAGE_CAPABILITIES = 0x00A0,
        LANGUAGE = 0x00B0,
        SOFTWARE_VERSION_LABEL = 0x00C0,
        BOOT_SOFTWARE_VERSION_ID = 0x00C1,
        BOOT_SOFTWARE_VERSION_LABEL = 0x00C2,

        //   Category - DMX512 Setup
        DMX_PERSONALITY = 0x00E0,
        DMX_PERSONALITY_DESCRIPTION = 0x00E1,
        DMX_START_ADDRESS = 0x00F0,
        SLOT_INFO = 0x0120,
        SLOT_DESCRIPTION = 0x0121,
        DEFAULT_SLOT_VALUE = 0x0122,

        //   Category – Sensors 
        SENSOR_DEFINITION = 0x0200,
        SENSOR_VALUE = 0x0201,
        RECORD_SENSORS = 0x0202,

        //   Category – Dimmer Settings


        //   Category – Power/Lamp Settings
        DEVICE_HOURS = 0x0400,
        LAMP_HOURS = 0x0401,
        LAMP_STRIKES = 0x0402,
        LAMP_STATE = 0x0403,
        LAMP_ON_MODE = 0x0404,
        DEVICE_POWER_CYCLES = 0x0405,

        //   Category - Display Settings
        DISPLAY_INVERT = 0x0500,
        DISPLAY_LEVEL = 0x0501,

        //   Category – Configuration
        PAN_INVERT = 0x0600,
        TILT_INVERT = 0x0601,
        PAN_TILT_SWAP = 0x0602,
        REAL_TIME_CLOCK = 0x0603,

        //   Category – Control
        IDENTIFY_DEVICE = 0x1000,
        RESET_DEVICE = 0x1001,
        POWER_STATE = 0x1010,
        PERFORM_SELFTEST = 0x1020,
        SELF_TEST_DESCRIPTION = 0x1021,
        CAPTURE_PRESET = 0x1030,
        PRESET_PLAYBACK = 0x1031,
    }

    public enum SensorType : byte {
        Temperature = 0x00,
        Voltage = 0x01,
        Current = 0x02,
        Frequency = 0x03,
        OutError = 0x04,
    }

    public enum SensorUnit : byte { 
        Centigrade = 0x01,
        Volts_DC = 0x02,
        Volts_AC_Peak = 0x03,
        Volts_AC_Rms = 0x04,
        Ampere_DC = 0x05,
        Ampere_AC_Peak = 0x06,
        Ampere_AC_Rms = 0x07,
        Hertz = 0x08,
    }

    public enum ArtFaultType { 
        Normal = 0x0,
        Undetect = 0x1,
    }

    /// <summary>
    /// Art分控单端口信息
    /// </summary>
    public class ArtPortInfo : JsonClassBase<ArtPortInfo> { 
        public string ip;
        public string mac;
        public int bindindex;
        public byte net;
        public byte address;
        public int universe { get {
            return net << 8 | address;
        } }
        public string[] UIDList;
        public byte[] nodeReport;
    }

    /// <summary>
    /// 单个Art设备传感器描述
    /// </summary>
    public class ArtSensorDef : JsonClassBase<ArtSensorDef> {
        public int ID;
        public SensorType Type;
        public SensorUnit Unit;
        public int Prefix;
        public int RangeMin;
        public int RangeMax;
        public int NormalMin;
        public int NormalMax;
        public bool RecordEn;
        public string Description;

        public float RealRangeMin { get { return Tools.Mult10Pow(RangeMin, -Prefix); } }
        public float RealRangeMax { get { return Tools.Mult10Pow(RangeMax, -Prefix); } }
        public float RealNormalMin { get { return Tools.Mult10Pow(NormalMin, -Prefix); } }
        public float RealNormalMax { get { return Tools.Mult10Pow(NormalMax, -Prefix); } }
    }

    /// <summary>
    /// 单个Art设备传感器值
    /// </summary>
    public class ArtSensorValue : JsonClassBase<ArtSensorValue> { 
        public int ID;
        public int CurrVal;
        public int LowestVal;
        public int HighestVal;
        public int RecordVal;
        public int Prefix = 0;

        public float RealCurrVal { get { return Tools.Mult10Pow(CurrVal, -Prefix); } }
        public float RealLowestVal { get { return Tools.Mult10Pow(LowestVal, -Prefix); } }
        public float RealHighestVal { get { return Tools.Mult10Pow(HighestVal, -Prefix); } }
        public float RealRecordVal { get { return Tools.Mult10Pow(RecordVal, -Prefix); } }
    }

    /// <summary>
    /// 单个Art设备详细信息
    /// </summary>
    public class ArtDeviceInfo : JsonClassBase<ArtDeviceInfo> {
        public string UID;

        public string Version;
        public string ModelID;
        public string Category;
        public string SoftwareVersionID;
        public string Footprint;
        public string Personality;
        public int DMXAddr;
        public int LampChannel;
        public int SubDeviceCount;
        public int SensorCount;
        public int ChipID;

        public List<ArtSensorInfo> SensorList;
        public string DeviceLabel;
        public string ManufactorLabel;
        public string SoftwareVersionLabel;
    }

    /// <summary>
    /// 单个Art设备传感器信息
    /// </summary>
    public class ArtSensorInfo : JsonClassBase<ArtSensorInfo> {
        public ArtSensorDef Definition;
        public ArtSensorValue Value;
    }

    /// <summary>
    /// 单端口Art设备信息
    /// </summary>
    public class ArtPortDetail : JsonClassBase<ArtPortDetail> {
        public int SubID;
        public int PortID;
        public int Universe;
        public List<ArtDeviceInfo> DeviceList;
    }

    /// <summary>
    /// Art设备链信息
    /// </summary>
    public class ArtDeviceChain : JsonClassBase<ArtDeviceChain> {
        public Dictionary<int, List<ArtPortDetail>> DetailList;
        public ArtDeviceChain() {
            DetailList = new Dictionary<int, List<ArtPortDetail>>();
        }
    }

    /// <summary>
    /// 单个Art设备错误详情
    /// </summary>
    public class ArtDeviceFault : JsonClassBase<ArtDeviceFault> {
        public int SubID;
        public int PortID;
        public string UID;
        public ArtFaultType FaultType;
        public DateTime FaultTime;
    }

    /// <summary>
    /// Art设备错误统计
    /// </summary>
    public class ArtFaultTotal : JsonClassBase<ArtFaultTotal> {
        public int Num {
            get {
                if (Details == null || Details.Count == 0) {
                    return 0;
                }
                return Details.Count;
            }
        }
        public List<ArtDeviceFault> Details;
    }
}
